package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import java.awt.Font;
import java.util.List;

import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.zretc.device.dao.Device_InDao;
import com.zretc.device.dao.Device_OutDao;
import com.zretc.device.dao.Device_ReturnDao;
import com.zretc.device.entity.Device_In;
import com.zretc.device.entity.Device_Out;
import com.zretc.device.entity.Device_Return;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPopupMenu;
import java.awt.Component;
import javax.swing.JMenuItem;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class Device_ReturnFindDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTable table;
	private JTextField txtInput;
	private JTextField txtreturn_person;
	private JTextField txtkeeper;
	private JTextField txtdamage_num;
	private JTextField txtnormal_num;
	private JTextField txtreturn_date;
	private JTextField txtdevice_code;
	private JTextField txtreturn_id;
	private JPopupMenu popupMenu;
	private JMenuItem mntmNewMenuItem;
	private String return_id;
	private JComboBox txtCB;
	private JLabel label_1;
	private JButton button_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Device_ReturnFindDialog dialog = new Device_ReturnFindDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Device_ReturnFindDialog() {
		setBounds(100, 100, 915, 620);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(33, 182, 796, 363);
		contentPanel.add(scrollPane);
		
		{
			{
				table = new JTable();
				table.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						// 取值
						return_id = table.getValueAt(table.rowAtPoint(e.getPoint()), 0) + "";
						String device_code = table.getValueAt(table.rowAtPoint(e.getPoint()), 1) + "";
						String return_date = table.getValueAt(table.rowAtPoint(e.getPoint()), 2) + "";
						String normal_num = table.getValueAt(table.rowAtPoint(e.getPoint()), 3) + "";
						String damage_num = table.getValueAt(table.rowAtPoint(e.getPoint()), 4) + "";
						String keeper = table.getValueAt(table.rowAtPoint(e.getPoint()), 5) + "";
						String return_person = table.getValueAt(table.rowAtPoint(e.getPoint()), 6) + "";
						// 赋值
						txtreturn_id.setText(return_id);
						txtdevice_code.setText(device_code);
						txtreturn_date.setText(return_date);
						txtnormal_num.setText(normal_num);
						txtdamage_num.setText(damage_num);
						txtkeeper.setText(keeper);
						txtreturn_person.setText(return_person);

					}
				});
				table.setRowHeight(25);
				{
					popupMenu = new JPopupMenu();
					addPopup(table, popupMenu);
					{
						mntmNewMenuItem = new JMenuItem("删除");
						mntmNewMenuItem.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent e) {

								// 让用户选择
								int n = JOptionPane.showConfirmDialog(null, "请问是否继续删除？删除后将无法恢复！",
										"删除出库单号为: " + return_id + " 的所有信息", JOptionPane.YES_NO_OPTION);
								if (n == 0) {
									// 根据单号删除
									Device_ReturnDao drd = new Device_ReturnDao();
									// 转型
									int returnId = Integer.parseInt(return_id);
									boolean flag = drd.delDevReturn(returnId);
									if (flag) {
										// 弹窗提示
										JOptionPane.showMessageDialog(null, "删除成功");
									} else {
										// 弹窗提示
										JOptionPane.showMessageDialog(null, "删除失败", "消息", JOptionPane.ERROR_MESSAGE);
									}
									// 更新
									fillTable(scrollPane);
								}
							}
						});
						popupMenu.add(mntmNewMenuItem);
					}
				}
				fillTable(scrollPane);
			}
		}
		{
			JLabel label = new JLabel("请输入查询信息");
			label.setFont(new Font("宋体", Font.PLAIN, 25));
			label.setBounds(33, 76, 202, 30);
			contentPanel.add(label);
		}
		{
			txtInput = new JTextField();
			txtInput.setFont(new Font("宋体", Font.PLAIN, 25));
			txtInput.setColumns(10);
			txtInput.setBounds(238, 76, 344, 30);
			contentPanel.add(txtInput);
		}
		{
			JButton button = new JButton("查询");
			button.setFont(new Font("宋体", Font.PLAIN, 20));
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// 获取复选框里的值
					String opString = txtCB.getSelectedItem() + "";
					// 获取文本框里的值
					String txt = txtInput.getText();
					if (txt.contentEquals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "查询内容不能为空");
					} else {
						if (opString.equals("还库单号")) {
							// 调用方法
							int returnId = Integer.parseInt(txt);
							Device_Return dr = fillTableById(scrollPane, returnId);
							if (dr == null) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "该单号不存在");
							}
						}
						else if (opString.equals("设备编号")) {
							// 调用方法
							List<Device_Return> list= fillTableByCode(scrollPane, txt);
							if (list.isEmpty()) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "该设备编号不存在");
							}
						}
						
						else if (opString.equals("归还日期")) {
							// 调用方法
							List<Device_Return> list= fillTableByDate(scrollPane, txt);
							if (list.isEmpty()) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "该归还日期不存在");
							}
						}
					}
				}
			});
			button.setBounds(596, 76, 113, 27);
			contentPanel.add(button);
		}
		{
			JButton button = new JButton("确认修改");
			button.setFont(new Font("宋体", Font.PLAIN, 20));
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// 还库编号
					String treturn_id = txtreturn_id.getText();
					// 设备编号
					String device_code = txtdevice_code.getText();
					// 归还日期
					String return_date = txtreturn_date.getText();
					// 可以正常使用的数量
					String tnormal_num = txtnormal_num.getText();
					// 损坏的数量
					String tdamage_num = txtdamage_num.getText();
					// 仓库管理者
					String keeper = txtkeeper.getText();
					// 归还人
					String return_person = txtreturn_person.getText();

					// 判断输入的值
					if (treturn_id.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "编号不能为空");
					} else if (device_code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "设备编号不能为空");
					} else if (return_date.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "归还日期不能为空");
					} else if (tnormal_num.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "正常数量不能为空");
					} else if (tdamage_num.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "损坏数量不能为空");
					} else if (keeper.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "仓库员工不能为空");
					} else if (return_person.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "归还人不能为空");
					} else {// 还库编号
						int return_id = Integer.parseInt(treturn_id);
						// 可以正常使用的数量
						int normal_num = Integer.parseInt(tnormal_num);
						// 损坏的数量
						int damage_num = Integer.parseInt(tdamage_num);
						// 创建对象
						Device_Return dreturn = new Device_Return(return_id, device_code, return_date, normal_num,
								damage_num, keeper, return_person);
						Device_ReturnDao drd = new Device_ReturnDao();
						// 调用增加的方法
						boolean flag = drd.modDevReturn(dreturn);
						if (flag) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "修改成功");
							fillTable(scrollPane);
						} else {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "修改失败");
						}
					}
				}
			});
			button.setBounds(652, 128, 113, 27);
			contentPanel.add(button);
		}
		{
			txtreturn_person = new JTextField();
			txtreturn_person.setColumns(10);
			txtreturn_person.setBounds(541, 128, 86, 30);
			contentPanel.add(txtreturn_person);
		}
		{
			txtkeeper = new JTextField();
			txtkeeper.setColumns(10);
			txtkeeper.setBounds(457, 128, 86, 30);
			contentPanel.add(txtkeeper);
		}
		{
			txtdamage_num = new JTextField();
			txtdamage_num.setColumns(10);
			txtdamage_num.setBounds(372, 128, 86, 30);
			contentPanel.add(txtdamage_num);
		}
		{
			txtnormal_num = new JTextField();
			txtnormal_num.setColumns(10);
			txtnormal_num.setBounds(287, 128, 86, 30);
			contentPanel.add(txtnormal_num);
		}
		{
			txtreturn_date = new JTextField();
			txtreturn_date.setColumns(10);
			txtreturn_date.setBounds(204, 128, 86, 30);
			contentPanel.add(txtreturn_date);
		}
		{
			txtdevice_code = new JTextField();
			txtdevice_code.setColumns(10);
			txtdevice_code.setBounds(118, 128, 86, 30);
			contentPanel.add(txtdevice_code);
		}
		{
			txtreturn_id = new JTextField();
			txtreturn_id.setEditable(false);
			txtreturn_id.setColumns(10);
			txtreturn_id.setBounds(33, 128, 86, 30);
			contentPanel.add(txtreturn_id);
		}
		{
			txtCB = new JComboBox();
			txtCB.setModel(new DefaultComboBoxModel(new String[] { "还库单号", "设备编号", "归还日期" }));
			txtCB.setFont(new Font("宋体", Font.PLAIN, 20));
			txtCB.setBounds(239, 31, 130, 30);
			contentPanel.add(txtCB);
		}
		{
			label_1 = new JLabel("请选择查询方式");
			label_1.setFont(new Font("宋体", Font.PLAIN, 25));
			label_1.setBounds(33, 25, 181, 38);
			contentPanel.add(label_1);
		}
		{
			button_1 = new JButton("取消");
			button_1.setFont(new Font("宋体", Font.PLAIN, 20));
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					txtInput.setText("");
					fillTable(scrollPane);
				}
			});
			button_1.setBounds(716, 76, 113, 27);
			contentPanel.add(button_1);
		}
		// 居中
		setLocationRelativeTo(null);
	}

	/**
	 * 查询所有
	 * 
	 * @param scrollPane
	 */
	private void fillTable(JScrollPane scrollPane) {
		// 创建对象
		Device_ReturnDao drd = new Device_ReturnDao();
		// 调用查找所有的方法
		List<Device_Return> list = drd.findDevReturnAll();
		int row = list.size();
		Object[][] obj = new Object[row][7];
		// 循环遍历
		for (int i = 0; i < obj.length; i++) {
			Device_Return dreturn = list.get(i);
			// 赋值
			obj[i][0] = dreturn.getReturn_id();
			obj[i][1] = dreturn.getDevice_code();
			obj[i][2] = dreturn.getReturn_date();
			obj[i][3] = dreturn.getNormal_num();
			obj[i][4] = dreturn.getDamage_num();
			obj[i][5] = dreturn.getKeeper();
			obj[i][6] = dreturn.getReturn_person();
		}

		table.setModel(new DefaultTableModel(obj,
				new String[] { "\u8FD8\u5E93\u5355\u53F7", "\u8BBE\u5907\u7F16\u53F7", "\u5F52\u8FD8\u65E5\u671F",
						"\u6B63\u5E38\u6570\u91CF", "\u635F\u574F\u6570\u91CF", "\u4ED3\u5E93\u5458\u5DE5",
						"\u5F52\u8FD8\u4EBA" }));
		scrollPane.setViewportView(table);
	}

	/**
	 * 根据单号
	 * 
	 * @param scrollPane
	 */
	private Device_Return fillTableById(JScrollPane scrollPane, int returnId) {
		// 创建对象
		Device_ReturnDao drd = new Device_ReturnDao();
		// 调用查找所有的方法
		Device_Return dreturn = drd.findDevReturnById(returnId);
		if (dreturn != null) {
			// 创建二维数组
			Object[][] obj = new Object[1][7];
			// 赋值
			obj[0][0] = dreturn.getReturn_id();
			obj[0][1] = dreturn.getDevice_code();
			obj[0][2] = dreturn.getReturn_date();
			obj[0][3] = dreturn.getNormal_num();
			obj[0][4] = dreturn.getDamage_num();
			obj[0][5] = dreturn.getKeeper();
			obj[0][6] = dreturn.getReturn_person();

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u8FD8\u5E93\u5355\u53F7", "\u8BBE\u5907\u7F16\u53F7", "\u5F52\u8FD8\u65E5\u671F",
							"\u6B63\u5E38\u6570\u91CF", "\u635F\u574F\u6570\u91CF", "\u4ED3\u5E93\u5458\u5DE5",
							"\u5F52\u8FD8\u4EBA" }));
			scrollPane.setViewportView(table);
		}
		return dreturn;
	}

	/**
	 * 根据设备编号模糊查询
	 * 
	 * @param scrollPane
	 */
	private List<Device_Return> fillTableByCode(JScrollPane scrollPane, String devCode) {
		// 创建对象
		Device_ReturnDao drd = new Device_ReturnDao();
		// 调用查找所有的方法
		List<Device_Return> list = drd.findDevReturnByCode(devCode);
		if (list != null) {
			int row = list.size();
			Object[][] obj = new Object[row][7];
			// 循环遍历
			for (int i = 0; i < obj.length; i++) {
				Device_Return dreturn = list.get(i);
				// 赋值
				obj[i][0] = dreturn.getReturn_id();
				obj[i][1] = dreturn.getDevice_code();
				obj[i][2] = dreturn.getReturn_date();
				obj[i][3] = dreturn.getNormal_num();
				obj[i][4] = dreturn.getDamage_num();
				obj[i][5] = dreturn.getKeeper();
				obj[i][6] = dreturn.getReturn_person();
			}

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u8FD8\u5E93\u5355\u53F7", "\u8BBE\u5907\u7F16\u53F7", "\u5F52\u8FD8\u65E5\u671F",
							"\u6B63\u5E38\u6570\u91CF", "\u635F\u574F\u6570\u91CF", "\u4ED3\u5E93\u5458\u5DE5",
							"\u5F52\u8FD8\u4EBA" }));
			scrollPane.setViewportView(table);
		}
		return list;
	}
	
	/**
	 * 根据还库日期模糊查询
	 * 
	 * @param scrollPane
	 */
	private List<Device_Return> fillTableByDate(JScrollPane scrollPane, String returnDate) {
		// 创建对象
		Device_ReturnDao drd = new Device_ReturnDao();
		// 调用查找所有的方法
		List<Device_Return> list = drd.findDevReturnByDate(returnDate);
		if (list != null) {
			int row = list.size();
			Object[][] obj = new Object[row][7];
			// 循环遍历
			for (int i = 0; i < obj.length; i++) {
				Device_Return dreturn = list.get(i);
				// 赋值
				obj[i][0] = dreturn.getReturn_id();
				obj[i][1] = dreturn.getDevice_code();
				obj[i][2] = dreturn.getReturn_date();
				obj[i][3] = dreturn.getNormal_num();
				obj[i][4] = dreturn.getDamage_num();
				obj[i][5] = dreturn.getKeeper();
				obj[i][6] = dreturn.getReturn_person();
			}

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u8FD8\u5E93\u5355\u53F7", "\u8BBE\u5907\u7F16\u53F7", "\u5F52\u8FD8\u65E5\u671F",
							"\u6B63\u5E38\u6570\u91CF", "\u635F\u574F\u6570\u91CF", "\u4ED3\u5E93\u5458\u5DE5",
							"\u5F52\u8FD8\u4EBA" }));
			scrollPane.setViewportView(table);
		}
		return list;
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
